package dcc.component;

import java.awt.Color;

import javax.swing.JComboBox;
import javax.swing.border.LineBorder;

import dcc.tools.Tools;

public class CustomComboBox extends JComboBox {

	public enum LoadType {
		FROMFILE, FROMDB
	}

	public CustomComboBox(LoadType loadType, String key) {
		this.setBorder(new LineBorder(Color.BLACK));
		if (loadType == LoadType.FROMFILE) {
			loadFromFile(key);
		} else if (loadType == LoadType.FROMDB) {
			loadFromDB(key);
		}
	}

	private void loadFromFile(String key) {
		String[] values = Tools.get().getValue(key).split(";");
		for (String value : values) {
			this.addItem(value);
		}
		setSelectedIndex(0);
	}

	private void loadFromDB(String key) {
//		Dictionary dictionary = ((DictionaryService) BeanFactory
//				.getBean("dictionaryService")).getByKey(key);
//		if (dictionary != null) {
//			List<DictionaryItem> values = ((DictionaryService) BeanFactory
//					.getBean("dictionaryService")).getAllValuesByKey(dictionary
//					.getId());
//			for (DictionaryItem item : values) {
//				this.addItem(item.getValue());
//			}
//			if(this.getItemCount() > 0){
//				setSelectedIndex(0);
//			}
//		}
	}
	
	public void setText(String text){
		this.setSelectedItem(text);
	}
}
